# -*- coding: utf-8 -*-
"""
Created on Wed Sep 16 14:03:37 2020

@author: Administrator
"""

'''
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]
Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

'''
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        
        
        '''
        20200916--李运辰
        不是很熟练，多看几遍
        '''
        
        if len(candidates)==0:
                return []
        
        candidates.sort()
        res=[]
        def helper(target,index,sub_list):
            if  target==0:
                res.append(sub_list)
            
            if target<0 or index>=len(candidates):
                 return
            
            for i in range(index,len(candidates)):
                if target < candidates[i]:
                    break
                helper(target-candidates[i],i,sub_list+[candidates[i]])
            
        helper(target,0,[])
        
        return res